*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

* Authors: Ranjit Lall & David Vilalta

* Paper: Varieties of Anxieties: Disaggregating Emotion and Voting Behavior in the COVID-19 Era

* What for: Replicates Tables and Figures For Madrid Election Section

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

clear all 

* Insert folder path
global folder ".../Insert folder path"


cd "${folder}"

*Local folders
global do_files "Do_Files/"
global dta "Final_data/"
global tables "Results/Tables/"
global figures "Results/Figures/"


use ${dta}Finaldata_Madrid, clear

global dem_controls D_population D_per_women D_per_age_0_20 D_per_age_21_35 D_per_age_36_50 D_per_age_51_65 D_per_age_66_100
global cov_controls nursingplaces_P Altitud agri_2 D_turnout
global ec_controls D_per_socsec pib_cap

global dem_controls_diff population per_women per_age_0_20 per_age_21_35 per_age_36_50 per_age_51_65 per_age_66_100 
global ec_controls_diff per_socsec 

********************************************************************************
* FIGURE 6
********************************************************************************

* TOP LEFT
reghdfe D_proconf c.cumcases##c.per_age_66_100 $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(per_age_66_100=(0.1 0.2 0.3 0.4 0.5) )
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 ytitle("Marginal Effect of Log COVID CPC " "on Pro-Lockdown Vote") ///
			 xtitle(Moderator: Elderly Share)

* BOTTOM LEFT
reghdfe D_anticonf c.cumcases##c.per_age_66_100 $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(per_age_66_100=(0.1 0.2 0.3 0.4 0.5) )
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 ytitle("Marginal Effect of Log COVID CPC on" "Anti-Lockdown Vote") ///
			 xtitle(Moderator: Elderly Share)

* TOP RIGHT
reghdfe D_proconf c.cumcases##c.deaths_resp_p $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(deaths_resp_p=(0 0.01 0.02 0.03))
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 yscale(range(-1.66 4)) ///
			 ylabel(-1(1)4) ///
			 ytitle("Marginal Effect of Log COVID CPC " "on Pro-Lockdown Vote") ///
			 xtitle(Moderator: Log Respiratory DPC)

* BOTTOM RIGHT
reghdfe D_anticonf c.cumcases##c.deaths_resp_p $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(deaths_resp_p=(0 0.01 0.02 0.03))
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 yscale(range(-4, 2)) ///
			 ylabel(-4(1)2) ///
			 ytitle("Marginal Effect of Log COVID CPC on" "Anti-Lockdown Vote") ///
			 xtitle(Moderator: Log Respiratory DPC)


********************************************************************************
* FIGURE 7
********************************************************************************

* TOP LEFT
reghdfe D_proconf c.cumcases##c.pib_p_hotels $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(pib_p_hotels=(0 1 2 3 4))
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 ytitle("Marginal Effect of Log COVID CPC " "on Pro-Lockdown Vote") ///
			 xtitle(Moderator: Hospitality Share)

* BOTTOM LEFT
reghdfe D_anticonf c.cumcases##c.pib_p_hotels $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(pib_p_hotels=(0 1 2 3 4))
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 ytitle("Marginal Effect of Log COVID CPC on" "Anti-Lockdown Vote") ///
			 xtitle(Moderator: Hospitality Share)
			 

* TOP RIGHT
reghdfe D_proconf c.cumcases##c.treat $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(treat=(0 1))
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 ytitle("Marginal Effect of Log COVID CPC " "on Pro-Lockdown Vote") ///
			 xtitle(Moderator: Top/Bottom Share)

* BOTTOM RIGHT
reghdfe D_anticonf c.cumcases##c.treat $dem_controls $cov_controls $ec_controls, a(NUTS4) cluster(NUTS4)
margins, dydx(cumcases) at(treat=(0 1))
marginsplot, plotopts(color(ltblue) mcolor(blue)) ///
			 ciopts(lcolor(blue) lwidth(medthick)) ///
			 yline(0, lcolor(red) lpattern(dash))  ///
			 graphregion(color(white)) ///
			 title("") ///
			 ytitle("Marginal Effect of Log COVID CPC on" "Anti-Lockdown Vote") ///
			 xtitle(Moderator: Top/Bottom Share)


********************************************************************************
* TABLE A14
********************************************************************************

* PANEL A (TOP)

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"


reghdfe D_`sub' c.cumcases##c.per_age_66_100, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.per_age_66_100 $dem_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.per_age_66_100 $dem_controls $cov_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.per_age_66_100 $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

}


* PANEL A (BOTTOM)

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

reghdfe D_`sub' c.cumcases##c.deaths_resp_p, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.deaths_resp_p $dem_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.deaths_resp_p $dem_controls $cov_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.deaths_resp_p $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

}


* PANEL B (TOP)

replace treat=0 if year==2019

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"


reghdfe D_`sub' c.cumcases##c.treat, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.treat $dem_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.treat $dem_controls $cov_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.treat $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

}


*PANEL B (BOTTOM)

replace pib_p_hotels=0 if year==2019

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"


reghdfe D_`sub' c.cumcases##c.pib_p_hotels, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.pib_p_hotels $dem_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.pib_p_hotels $dem_controls $cov_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.pib_p_hotels $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

}

********************************************************************************
* TABLE A15
********************************************************************************
				
forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

reghdfe D_`sub' c.cumcases##c.per_age_66_100 c.cumcases##c.pib_p_hotels $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.per_age_66_100 c.cumcases##c.treat $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.deaths_resp_p c.cumcases##c.pib_p_hotels $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

reghdfe D_`sub' c.cumcases##c.deaths_resp_p c.cumcases##c.treat $dem_controls $cov_controls $ec_controls, a(i.NUTS4) cluster(NUTS4)

}		

		

********************************************************************************
* TABLE A16
********************************************************************************

use ${dta}Finaldata_Madrid, clear


* PANEL A (TOP)

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"


reghdfe `sub' c.cumcases##c.per_age_66_100, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.per_age_66_100 $dem_controls_diff, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.per_age_66_100 $dem_controls_diff $ec_controls_diff, a(i.mun_s i.year) cluster(mun_s)

}


* PANEL A (BOTTOM)

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

reghdfe `sub' c.cumcases##c.deaths_resp_p, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.deaths_resp_p $dem_controls_diff, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.deaths_resp_p $dem_controls_diff $ec_controls_diff, a(i.mun_s i.year) cluster(mun_s)

}

		
* PANEL B (TOP)
replace treat=0 if year==2019

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

reghdfe `sub' c.cumcases##c.treat, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.treat $ec_controls_diff, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.treat $dem_controls_diff $ec_controls_diff, a(i.mun_s i.year) cluster(mun_s)

}

* PANEL B (BOTTOM)
replace pib_p_hotels=0 if year==2019

forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

reghdfe `sub' c.cumcases##c.pib_p_hotels, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.pib_p_hotels $dem_controls_diff, a(i.mun_s i.year) cluster(mun_s)

reghdfe `sub' c.cumcases##c.pib_p_hotels $dem_controls_diff $ec_controls_diff, a(i.mun_s i.year) cluster(mun_s)

}



********************************************************************************
* FIGURE A9
********************************************************************************

use ${dta}Finaldata_Madrid_Commontrends, clear

* PANEL A
preserve
collapse proconfin, by(year t2021_1 t2021_2 t2021_3 t2021_4)
tw (connected proconfin year if t2021_1) (connected proconfin year if t2021_2) (connected proconfin year if t2021_3) (connected proconfin year if t2021_4), legend(ring(1) order(1 "First Quartile" 2 "Second Quartile" 3 "Third Quartile" 4 "Fourth Quartile")) ytitle(Vote Share of Pro-Lockdown Parties) xtitle (Year) xline(2020, lpattern(dash)) graphregion(color(white))
restore

* PANEL B
preserve
collapse proconfin, by(year covid)
tw (connected proconfin year if covid) (connected proconfin year if  !covid), legend(ring(1) order(1 "Above Median" 2 "Below Median")) ytitle(Vote Share of Pro-Lockdown Parties) xtitle (Year) xline(2020, lpattern(dash)) graphregion(color(white))
restore

* PANEL C
preserve
collapse anticonfin, by(year t2021_1 t2021_2 t2021_3 t2021_4)
tw (connected anticonfin year if t2021_1) (connected anticonfin year if t2021_2) (connected anticonfin year if t2021_3) (connected anticonfin year if t2021_4), legend(ring(1) order(1 "First Quartile" 2 "Second Quartile" 3 "Third Quartile" 4 "Fourth Quartile")) ytitle(Vote Share of Anti-Lockdown Parties) xtitle (Year) xline(2020, lpattern(dash)) graphregion(color(white))
restore

* PANEL D
preserve
collapse anticonfin, by(year covid)
tw (connected anticonfin year if covid) (connected anticonfin year if  !covid), legend(ring(1) order(1 "Above Median" 2 "Below Median")) ytitle(Vote Share of Anti-Lockdown Parties) xtitle (Year) xline(2020, lpattern(dash)) graphregion(color(white))
restore


********************************************************************************
* TABLE A17
********************************************************************************

use ${dta}Finaldata_Madrid, clear

global rain rain_6mc
global temp temp_6mc
global wind wind_6mc
global ivhealth c.$rain##c.$wind##c.ivhealth c.$temp##c.ivhealth c.$rain##c.ivhealth c.$wind##c.ivhealth 
global ivecon c.$rain##c.$wind##c.ivecon c.$temp##c.ivecon c.$rain##c.ivecon c.$wind##c.ivecon 

global comb_controls D_population D_per_women D_per_age_0_20 D_per_age_21_35 D_per_age_36_50 D_per_age_51_65 D_per_age_66_100 nursingplaces_P Altitud agri_2 D_per_socsec pib_cap
  
  
* PANEL A (ELDERLY SHARE)		
gen ivhealth=per_age_66_100
forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

ivreg2 D_`sub' (cumcases c.cumcases#c.ivhealth=$ivhealth) ivhealth $comb_controls i.NUTS4, cluster(NUTS4) first savefirst savefprefix(first_`sub'_1)

}
drop ivhealth

* PANEL A (LOG RESPIRATORY DPC)		
rename deaths_resp_p ivhealth
forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

ivreg2 D_`sub' (cumcases c.cumcases#c.ivhealth=$ivhealth) ivhealth $comb_controls i.NUTS4, cluster(NUTS4) first savefirst savefprefix(first_`sub'_4)

}
rename ivhealth deaths_resp_p


* PANEL B (TOP/BOTTOM INCOME)		

rename treat ivecon
forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

ivreg2 D_`sub' (cumcases c.cumcases#c.ivecon=$ivecon) ivecon $comb_controls i.NUTS4, cluster(NUTS4) first savefirst savefprefix(first_`sub'_2)

}
rename ivecon treat


* PANEL B (HOSPITALITY SHARE)		
rename pib_p_hotels ivecon
forvalues i=1/2{
if `i'==1 local sub="proconf"
if `i'==2 local sub="anticonf"

ivreg2 D_`sub' (cumcases c.cumcases#c.ivecon=$ivecon) ivecon $comb_controls i.NUTS4, cluster(NUTS4) first savefirst savefprefix(first_`sub'_3)

}
rename ivecon pib_p_hotels

